package com.aatattoo.light_food_service.controller;

import com.aatattoo.light_food_service.entity.DynamicImg;
import com.aatattoo.light_food_service.entity.User;
import com.aatattoo.light_food_service.mapper.DynamicMapper;
import com.aatattoo.light_food_service.service.DynamicImgService;
import com.aatattoo.light_food_service.service.TokenService;
import com.aatattoo.light_food_service.service.UserService;
import com.aatattoo.light_food_service.utils.UploadUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@Controller
@ResponseBody
@RequestMapping("/upload")
public class UploadController {
    @Autowired
    private UserService userService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private DynamicImgService dynamicImgService;

    @PostMapping("/avatar")
    public String setUserImg(@RequestParam MultipartFile userImg) throws IOException {
        //获取原始文件名
        String originalFilename = userImg.getOriginalFilename();
        String uuidFilename = UploadUtils.getUUIDName(originalFilename);
        File fileDir = new File("/uploadfiles/userImg");
//        File fileDir = new File("/Users/aatattoo/Desktop/file");
        if (!fileDir.exists()) {
            fileDir.mkdirs();
        }
        File newFile = new File("/uploadfiles/userImg" , uuidFilename);
//        File newFile = new File("/Users/aatattoo/Desktop/file" , uuidFilename);
        if (!newFile.exists()){
            newFile.mkdirs();
        }
        //将文件输出到目标的文件中
        userImg.transferTo(newFile);
        String savePath =  "/avatar/" + uuidFilename;
        return savePath;
    }

    @PostMapping("/newAvatar")
    public String changeUserImg(@RequestParam MultipartFile userImg,@RequestHeader("token")String token) throws IOException {
        //获取原始文件名
        String originalFilename = userImg.getOriginalFilename();
        String uuidFilename = UploadUtils.getUUIDName(originalFilename);
        File fileDir = new File("/uploadfiles/userImg");
//        File fileDir = new File("/Users/aatattoo/Desktop/file");
        if (!fileDir.exists()) {
            fileDir.mkdirs();
        }
        File newFile = new File("/uploadfiles/userImg" , uuidFilename);
//        File newFile = new File("/Users/aatattoo/Desktop/file" , uuidFilename);
        if (!newFile.exists()){
            newFile.mkdirs();
        }
        //将文件输出到目标的文件中
        userImg.transferTo(newFile);
        String savePath =  "/user/avatar/" + uuidFilename;
        int userId = tokenService.analysisUserId(token);
        System.out.println(userId);
        User user=new User();
        user.setId(userId);
        user.setAvatar(savePath);
        userService.updataAvatarById(user);
        return savePath;
    }

    @PostMapping("/dynamicImg")
    public List<Object> dynamicImg(@RequestParam MultipartFile[] img) throws IOException {
         File fileDir = new File("/uploadfiles/dynamicImg");
//        File fileDir = new File("/Users/aatattoo/Desktop/file");
        if (!fileDir.exists()) {
            fileDir.mkdirs();
        }
        List<Object> imgList=new ArrayList<>();
        for (int i = 0; i < img.length; i++) {
            //获取原始文件名
            String originalFilename = img[i].getOriginalFilename();
            String uuidFilename = UploadUtils.getUUIDName(originalFilename);
            File newFile = new File("/uploadfiles/dynamicImg" , uuidFilename);
//            File newFile = new File("/Users/aatattoo/Desktop/file" , uuidFilename);
            if (!newFile.exists()){
                newFile.mkdirs();
            }
            img[i].transferTo(newFile);
            //将文件输出到目标的文件中
            String savePath =  "/dynamic/img/" + uuidFilename;
            DynamicImg dynamicImg=new DynamicImg();
            dynamicImg.setAddress(savePath);
            dynamicImg.setOrder(i+1);
            int imgID = dynamicImgService.addDynamicImg(dynamicImg);
            imgList.add(imgID);
        }
        return imgList;
    }

}
